package com.Babysleep.manage;

import android.content.SharedPreferences;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.Babysleep.R;
import com.Babysleep.Activity.BasicActivity;
import com.Babysleep.Interface.ListenHandler;

public class YaoLanQuPlay extends Listen {

	private String[] pathes;
	MyHandler handler;
	TextView daojishi;
	public YaoLanQuPlay(BasicActivity activity) {
		super(activity);
		daojishi=(TextView)activity.findViewById(R.id.daojishi);
		handler = new MyHandler(Constant.delay_length);
	}

	
	
	
	@Override
	public void init(ImageButton button, int path_id) {
		switch (path_id) {
		case R.id.text1:
			super.init(button, pathes[0]);
			break;
		case R.id.text2:
			super.init(button, pathes[1]);
			break;
		case R.id.text3:
			super.init(button, pathes[2]);
			break;
		case R.id.text4:
			super.init(button, pathes[3]);
			break;
		case R.id.text5:
			super.init(button, pathes[4]);
			break;
		default:
			break;
		}
		
	}




	public void load_file() {
		SharedPreferences setting = activity.getSharedPreferences("settings", 0);
		String name = setting.getString("song_name", null);
		pathes=new String[5];
		pathes[0]=name;
		pathes[1]=name;
		pathes[2]=name;
		pathes[3]=name;
		pathes[4]=name;
	}
	
	@Override
	public void Play() {
		// TODO Auto-generated method stub
		if (playing_started == false) {
			System.out.println("laile");
			handler.start();
		}
		super.Play();
	}

	@Override
	public void Pause() {
		// TODO Auto-generated method stub
		super.Pause();// 播放控制
		handler.pause();// handler控制
	}

	@Override
	public void Restart() {
		// TODO Auto-generated method stub
		super.Restart();
		handler.restart();
	}

	@Override
	public void Stop() {
		// TODO Auto-generated method stub
		super.Stop();
		handler.stop();
	}

	public class MyHandler extends ListenHandler {

		public MyHandler(int delay) {
			super(delay);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (need_send == true) {
					// UI();
					if (activity.cycle_length != 0)
						count();
					else if (daojishi.getVisibility() == View.VISIBLE)
						daojishi.setText(df.format(0));
					sendEmptyMessageDelayed(1, delay);
				}
				break;

			default:
				break;
			}
		}

	}

	public void count() {
		super.count();
		System.out.println(counter);
		if (daojishi.getVisibility() == View.VISIBLE) {
			((TextView) activity.findViewById(R.id.daojishi)).setText(df
					.format(counter));
			if (counter <= 0) {
				((TextView) activity.findViewById(R.id.daojishi)).setText(df
						.format(0));
				Stop();
			}
		} else if (counter <= 0)
			Stop();
	}

	
	
	
}